package user

import (
	"time"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
	"gitee.com/xiaoyutab/xgotool/individual/xginplus"
	"gitee.com/xiaoyutab/xgotool/individual/xqueue"
)

// 修改用户基础信息
//
//	POST	/user/update
func Update(c *xginplus.Context) *xginplus.Response {
	mod := model.User{}
	err := c.Bind(&mod)
	if err != nil {
		return c.Error(err)
	}
	if mod.Id != c.GetUint("uid") {
		return c.Errorf("暂无权限")
	}
	conn, err := config.GetConnect()
	if err != nil {
		return c.Error(err)
	}
	err = conn.Table(mod.TableName()).
		Where("id", c.GetUint("uid")).
		Where("status", 1).
		Select("nickname", "phone", "email").
		Updates(&mod).
		Error
	if err != nil {
		return c.Error(err)
	}
	xqueue.Set("log.user", &model.UserLog{
		Type: 3,
		Uid:  c.GetUint("uid"),
		Ip:   c.ClientIP(),
		Ua:   c.Request.UserAgent(),
		Time: time.Now().Unix(),
	})
	return c.SuccessEmpty()
}
